package com.hongxinguoji.manager.article.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongxinguoji.manager.article.pojo.HxArticle;
import com.hongxinguoji.manager.article.pojo.HxArticleExt;
import com.hongxinguoji.manager.article.service.IArticleDetailService;
import com.hongxinguoji.manager.article.service.IArticleService;
import com.hongxinguoji.manager.common.util.EasyUIDataGridResult;
import com.hongxinguoji.manager.common.util.HXResult;
import com.hongxinguoji.manager.common.util.IDUtils;

/**
 * 文章操作相关controller
 * 
 * @author 荆志宝
 *
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private IArticleService articleService;

	@Autowired
	private IArticleDetailService articleDetailService;

	@RequestMapping("/articleSave")
	@ResponseBody
	public HXResult addArticle(HxArticle article, String content) {
		long articleId = IDUtils.getArticleId();
		// 设置文章ID
		article.setId(articleId);
		// 设置文章状态
		article.setStatus(1);
		Date date = new Date();
		// 设置时间
		if (article.getUpdateTime() == null) {
			article.setUpdateTime(date);
		}
		article.setCreateTime(date);
		try {
			articleService.addArticle(article, content);
			return HXResult.ok("发布文章成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "发布文章失败！");
		}

	}

	@RequestMapping("/list")
	@ResponseBody
	public EasyUIDataGridResult getArticleExtList(Integer page, Integer rows) {

		EasyUIDataGridResult result = new EasyUIDataGridResult();

		// 分页处理
		PageHelper.startPage(page, rows);
		List<HxArticleExt> list = articleService.getArticleExtList();
		// 查询所有数据
		PageInfo<HxArticleExt> pageInfo = new PageInfo<>(list);

		result.setTotal(pageInfo.getTotal());
		result.setRows(list);

		return result;
	}

	@RequestMapping("/findbyproperty")
	@ResponseBody
	public EasyUIDataGridResult getListByProperty(Integer page, Integer rows, String name, String value) {

		EasyUIDataGridResult result = new EasyUIDataGridResult();

		// 分页处理
		PageHelper.startPage(page, rows);
		List<HxArticleExt> list = articleService.getArticleExtListByProperty(name, value);
		// 查询所有数据
		PageInfo<HxArticleExt> pageInfo = new PageInfo<>(list);

		result.setTotal(pageInfo.getTotal());
		result.setRows(list);

		return result;
	}

	@RequestMapping("/lock")
	@ResponseBody
	public HXResult lockArticle(String ids) {
		try {
			// 截取字符串
			String[] idStrings = ids.split(",");
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				articleService.lockArticle(ids);
			}

			return HXResult.ok("锁定文章成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "锁定文章失败！");
		}

	}

	@RequestMapping("/unlock")
	@ResponseBody
	public HXResult restoreArticle(String ids) {
		try {
			// 截取字符串
			String[] idStrings = ids.split(",");
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				articleService.lockArticle(ids);
			}

			return HXResult.ok("解锁文章成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "解锁文章失败！");
		}
	}

	@RequestMapping("/editArticle")
	@ResponseBody
	public HxArticleExt getArticle(Long id) {
		return articleService.getArticleExtById(id);
	}

	@RequestMapping("/articleDetail")
	@ResponseBody
	public HXResult getArticleDetailByID(Long id) {
		return articleDetailService.getArticleDetailById(id);
	}

	@RequestMapping("/update")
	@ResponseBody
	public HXResult updateArticle(HxArticle article, String content, String createDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(createDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		article.setCreateTime(date);

		return articleService.updateArticle(article, content);

	}

	@RequestMapping("/delete")
	@ResponseBody
	public HXResult deleteArticle(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);

				articleService.deleteArticle(i);
			}
			return HXResult.ok("内容删除成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "内容删除失败！");
		}

	}

	// 文章页面跳转
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		return "article/" + path;
	}
}
